<?php
class b2c_mdl_business_orders_objects extends dbeav_model{
    /**
     * 订单明细
     * @param $order_id
     */
    public function get_confirm_order_objects($order_id,$filter){
        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,B.createtime,D.brand_name,C.unit_num,C.s_price,C.cost as s_cost,(A.buy_price*(1-D.rebate/100))as cost,F.total_confirm_num,G.chinese_all_name FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_business_orders AS B ON A.childOrder_id=B.childOrder_id LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id LEFT JOIN sdb_b2c_brand AS D ON C.brand_id=D.brand_id LEFT JOIN (SELECT sum(E.has_confirm_num) AS total_confirm_num,E.goods_id FROM sdb_b2c_business_orders_objects as E GROUP BY E.goods_id) AS F ON A.goods_id = F.goods_id LEFT JOIN sdb_b2c_business_basicmsg AS G ON A.business_id=G.business_id WHERE A.mainOrder_id='".$order_id."' and A.state='0' and B.order_status<>'dead'";
        if($filter && isset($filter['business_id'])){
            $sql .= " and A.business_id=".$filter['business_id'];
        }
        return $this->db->select($sql);
    }

    //liuyulei 排序
    public function get_business_order_objects_order($business_id, $state, $order, $limit){
        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,B.createtime,D.brand_name,C.unit_num,C.s_price,C.cost as s_cost,round((A.buy_price*(1-D.rebate/100)),3) as cost,F.total_confirm_num,G.chinese_all_name
        FROM sdb_b2c_business_orders_objects AS A
        LEFT JOIN sdb_b2c_business_orders AS B ON A.childOrder_id=B.childOrder_id
        LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id
        LEFT JOIN sdb_b2c_brand AS D ON C.brand_id=D.brand_id
        LEFT JOIN (SELECT sum(E.has_confirm_num) AS total_confirm_num,E.goods_id,H.c_status FROM sdb_b2c_business_orders_objects as E LEFT JOIN sdb_b2c_orders AS H ON H.order_id = E.mainOrder_id WHERE H.status='active' and H.c_status <> '3' GROUP BY E.goods_id) AS F ON A.goods_id = F.goods_id
        LEFT JOIN sdb_b2c_business_basicmsg AS G ON A.business_id=G.business_id
        WHERE A.business_id=".$business_id."
        and A.state='".$state."'
        and B.order_status<>'dead'
        and C.marketable = 'true'
        order by A.".$order.$limit;
        return $this->db->select($sql);
    }
    /**
     * 订单明细 by yyj 2015/12/4
     * @param $business_id
     */
    public function get_business_order_objects($business_id, $limit){
        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,B.createtime,D.brand_name,C.unit_num,C.s_price,C.cost as s_cost,round((A.buy_price*(1-D.rebate/100)),3) as cost,F.total_confirm_num,G.chinese_all_name FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_business_orders AS B ON A.childOrder_id=B.childOrder_id LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id LEFT JOIN sdb_b2c_brand AS D ON C.brand_id=D.brand_id LEFT JOIN (SELECT sum(E.has_confirm_num) AS total_confirm_num,E.goods_id,H.c_status FROM sdb_b2c_business_orders_objects as E LEFT JOIN sdb_b2c_orders AS H ON H.order_id = E.mainOrder_id WHERE H.status='active' and H.c_status <> '3' GROUP BY E.goods_id) AS F ON A.goods_id = F.goods_id LEFT JOIN sdb_b2c_business_basicmsg AS G ON A.business_id=G.business_id WHERE A.business_id=".$business_id." and A.state='0' and B.order_status<>'dead' order by A.bn desc".$limit;
        return $this->db->select($sql);
    }

    public function gethas_business_order_objects($business_id, $limit){
        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,B.createtime,D.brand_name,C.unit_num,C.s_price,C.cost as s_cost,round((A.buy_price*(1-D.rebate/100)),3) as cost,F.total_confirm_num,G.chinese_all_name FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_business_orders AS B ON A.childOrder_id=B.childOrder_id LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id LEFT JOIN sdb_b2c_brand AS D ON C.brand_id=D.brand_id LEFT JOIN (SELECT sum(E.has_confirm_num) AS total_confirm_num,E.goods_id,H.c_status FROM sdb_b2c_business_orders_objects as E LEFT JOIN sdb_b2c_orders AS H ON H.order_id = E.mainOrder_id WHERE H.status='active' and H.c_status <> '3' GROUP BY E.goods_id) AS F ON A.goods_id = F.goods_id LEFT JOIN sdb_b2c_business_basicmsg AS G ON A.business_id=G.business_id WHERE A.business_id=".$business_id." and A.state='1' and B.order_status<>'dead' order by A.bn desc".$limit;
        return $this->db->select($sql);
    }

    /**
     * 以商品整合订单明细 by yyj 2015/12/9
     * @param $business_id,$ship_status
     */
    public function get_business_order_objects_ship($business_id,$ship_status){
        $sql = "SELECT A.*,sum(A.has_confirm_num) as total_confirm_num FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_orders AS B ON A.mainOrder_id = B.order_id WHERE A.business_id='".$business_id."' and B.ship_status in(".$ship_status.") GROUP BY A.goods_id ORDER BY A.bn,A.childOrder_id";
        return $this->db->select($sql);
    }


    /**
     * 重写getList方法
    //   */
//    public function get_finder($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null,&$total=false)
//    {
//        if($_GET['view']==0){
//            $where = ' o.state=\'0\' and bo.order_status=\'active\'';
//        }else{
//            $where = ' o.state=\'1\' and bo.order_status<>\'dead\'';
//        }
//        $sql = 'SELECT count(o.obj_id) as num,b.english_all_name,b.operator_id,o.business_id,o.state,u.user_id,u.name
//                FROM `sdb_b2c_business_orders_objects` as o
//                left join `sdb_b2c_business_basicmsg` as b
//                on o.business_id=b.business_id
//                left join `sdb_desktop_users` as u
//                on b.operator_id=u.user_id
//                LEFT JOIN `sdb_b2c_business_orders` AS bo
//                ON bo.childOrder_id=o.childOrder_id
//                WHERE '.$where.' ';
//        //搜索供应商名称---jyy 2016/10/25
//        if($filter && isset($filter['english_all_name'])){
//            $sql .= " and b.english_all_name like '%".$filter['english_all_name'].'%\'';
//        }
//        if($filter && isset($filter['name'])){
//            $sql .= " and u.name like '%".$filter['name'].'%\'';
//        }
//        $sql .= " group by o.business_id order by o.obj_id desc ";
//        $data = $this->db->selectLimit($sql,$limit,$offset);
//        return $data;
//    }

    /**
     * 供应商订单列表供应商、操作员搜索
     * author jyy 2016/10/24
     */
    public function searchOptions(){
        $columns = array();
        foreach($this->_columns() as $k=>$v){
            if(isset($v['searchtype']) && $v['searchtype']){
                $columns[$k] = $v['label'];
            }
        }
        /** 添加供应商名称搜索 **/
        $columns['english_all_name'] = app::get('b2c')->_('供应商名称');
//        $columns['name'] = app::get('b2c')->_('操作员');
        /** end **/

        return $columns;
    }

    /**
     * 供应商订单列表供应商数量统计count
     * author jyy 2016/10/27
     */
//    public function count($filter=null){
//        $filter = "1";
//        $row = $this->db->select('SELECT count( DISTINCT obj_id) as _count FROM sdb_b2c_business_orders_objects
//        WHERE '.$filter);
//        return intval($row[0]['_count']);
//    }
//
//    public function get_finder_count(){
//        return count($this->get_finder('*', array(),  0,  -1));
//    }


    /**
     * 重写getList方法
     * author jyy 2016/10/27
     */
    public function get_finder_objects($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null,&$total=false)
    {
        if($_GET['view']==0){
            $where = ' A.state=\'0\' and B.order_status=\'active\'';
        }else{
            $where = ' A.state=\'1\' and B.order_status=\'active\'';
        }
        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,B.createtime,D.brand_name,
                C.unit_num,C.s_price,C.cost as s_cost,(A.buy_price*(1-D.rebate/100))as cost,
                G.english_all_name FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_business_orders AS B
                ON A.childOrder_id=B.childOrder_id LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id
                LEFT JOIN sdb_b2c_brand AS D ON C.brand_id=D.brand_id LEFT JOIN sdb_b2c_business_basicmsg AS G ON A.business_id=G.business_id
                WHERE $where";
        if($filter && isset($filter['obj_id'])){
            $sql .= " and A.obj_id = ".$filter['obj_id'];
        }
        //快捷搜索供应商名称---jyy 2016/10/25
        if($filter && isset($filter['english_all_name'])){
            $sql .= " and G.english_all_name like '%".$filter['english_all_name'].'%\'';
        }
        //高级筛选filter
        if($filter && isset($filter['business_id'])){
            $sql .= " and A.business_id = ".$filter['business_id'];
        }
        if($filter && isset($filter['mainOrder_id'])){
            $sql .= " and A.mainOrder_id = ".$filter['mainOrder_id'];
        }
        //货品编号
        if($filter && isset($filter['bn'])){
            $sql .= " and A.bn = '".$filter['bn'].'\'';
        }
        $sql .= " order by A.createtime desc ";
        $data = $this->db->selectLimit($sql,$limit,$offset);

        return $data;
    }


    public function get_count(){
        return count($this->get_finder_objects('*', array(),  0,  -1));
    }

    public function get_finder_objects_export($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null,&$total=false)
    {
        //if($_GET['view']==0){
        //$where = ' A.state=\'0\' and B.order_status=\'active\'';
        //}else{
        //$where = ' A.state=\'1\' and B.order_status<>\'dead\'';
        //}
        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,B.createtime,D.brand_name,
                C.unit_num,C.s_price,C.cost as s_cost,(A.buy_price*(1-D.rebate/100))as cost,
                G.english_all_name FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_business_orders AS B
                ON A.childOrder_id=B.childOrder_id LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id
                LEFT JOIN sdb_b2c_brand AS D ON C.brand_id=D.brand_id LEFT JOIN sdb_b2c_business_basicmsg AS G ON A.business_id=G.business_id
                WHERE B.order_status='active' ";
        if($filter && isset($filter['obj_id'])){
            $sql .= " and A.obj_id = ".$filter['obj_id'];
        }
        //快捷搜索供应商名称---jyy 2016/10/25
        if($filter && isset($filter['english_all_name'])){
            $sql .= " and G.english_all_name like '%".$filter['english_all_name'].'%\'';
        }
        //高级筛选filter
        if($filter && isset($filter['business_id'])){
            $sql .= " and A.business_id = ".$filter['business_id'];
        }
        if($filter && isset($filter['mainOrder_id'])){
            $sql .= " and A.mainOrder_id = ".$filter['mainOrder_id'];
        }
        //货品编号
        if($filter && isset($filter['bn'])){
            $sql .= " and A.bn = '".$filter['bn'].'\'';
        }
        $sql .= " order by A.createtime desc ";
        $data = $this->db->selectLimit($sql,$limit,$offset);

        return $data;
    }
}